<?php
/*
 * Copyright(c) 2011 SOFTBANK PAYMENT SERVICE Corp. All rights reserved.
 * http://www.sbpayment.jp/
 * Copyright(c) 2000-2009 LOCKON CO.,LTD. All Rights Reserved.
 * http://www.lockon.co.jp/
 */
require_once('../system/sbps/request.php');
require_once('../system/sbps/XML/Serializer.php');
require_once('../system/sbps/XML/Unserializer.php');

/**
 * SPS用各種ユーティリティクラス
 */
class LC_Util_SPS {
    /**
     * SPSへリクエストを送信
     * @param string $requestXml
     * @param array $spsSetting
     * @return string $r_code
     * @return string $response
	*/
    function sendRequestSps($requestXml,$order_url,$id_sps_marchant,$id_sps_service,$id_sps_hash) {
        // リクエスト処理
        $req = new HTTP_Request($order_url);
        $req->addHeader('Content-Type', 'text/xml');
        $req->setMethod('POST');
        $req->setBasicAuth($id_sps_marchant. $id_sps_service, $id_sps_hash);
        $req->setBody($requestXml);
        $req->sendRequest();
        $r_code = $req->getResponseCode();
        $response = $req->getResponseBody();

        return array($r_code, $response);
    }

    /**
     * 配列をXML化
     * @param array $arrXml
     * @param string $request_code
     * @return string $xml
     */
    function changeArray2Xml($arrXml, $request_code) {
        // 配列をXML化
        $objXmlSerializer = new XML_Serializer();
        $objXmlSerializer->setOption("indent", "    ");
        $objXmlSerializer->setOption("addDecl", true);
        $objXmlSerializer->setOption("encoding", "Shift_JIS");
        $objXmlSerializer->setOption("rootName", "sps-api-request");
        $objXmlSerializer->setOption("rootAttributes", array("id" => $request_code));
        $objXmlSerializer->serialize($arrXml);
        $xml = $objXmlSerializer->getSerializedData();

        return $xml;
    }

    /**
     * XMLを配列化
     * @param string $xml
     * @return array $arrXml
     */
    function changeXml2Array($xml) {
        // 不要なデータを排除
        $xml = preg_replace("/<\?xml.*\?>/", "", $xml);

        // XMLを配列化
        $objXmlUnserializer = new XML_Unserializer();
        $objXmlUnserializer->setOption("parseAttributes", true);
        $objXmlUnserializer->unserialize($xml);
        $arrXml = $objXmlUnserializer->getUnserializedData();

        return $arrXml;
    }

    /**
     * ハッシュ生成
     * @param array $arrData
     * @param array $spsSetting
     * @return string $hashcode
     */
    function createHashcode($arrData, $id_sps_hash) {
    	$hashcode="";
        foreach ($arrData as $key1 => $val1) {
            if (is_array($val1) === false) {
                $hashcode .= trim($val1);
            } else {
                foreach ($val1 as $key2 => $val2) {
                    $hashcode .= trim($val2);
                }
            }
        }
        $hashcode = sha1($hashcode. trim($id_sps_hash));

        return $hashcode;
    }

    /**
     * 暗号化
     * @param array $arrData
     * @param string $pkey
     * @param array $spsSetting
     * @return array $arrData
     */
    function encryptData($arrData, $pkey, $key_sps_3des,$key_sps_3des_iv) {
        // パディング（最後の8バイトブロックに半角スペースを補完）
        foreach ($arrData[$pkey] as $key => $val) {
            $val_len = strlen($val);
            if ($val_len > 0) {
                $val_last_8byte = $val_len % 8;
                $val_null_8byte = ($val_last_8byte != 0) ? 8 - $val_last_8byte : 0;
                for ($i = 0; $i < $val_null_8byte; $i++) {
                    $arrData[$pkey][$key] .= " ";
                }
            }
        }

        // 暗号化
        $em = mcrypt_module_open(MCRYPT_3DES, "",  MCRYPT_MODE_CBC, "");
        foreach ($arrData[$pkey] as $key => $val) {
            if (strlen($val) > 0) {
                mcrypt_generic_init($em, $key_sps_3des, $key_sps_3des_iv);
                $encrypt_val = mcrypt_generic($em, $val);
                mcrypt_generic_deinit($em);
                $arrData[$pkey][$key] = base64_encode($encrypt_val);
            }
        }
        mcrypt_module_close($em);

        return $arrData;
    }

    /**
     * マルチバイト文字列のエンコード
     * @param array $arrData
     * @param array $key
     * @return array $arrData
     */
    function encodeMbstring($arrData, $arrKey) {
        foreach ($arrKey as $key) {
            $arrData[$key] = base64_encode($arrData[$key]);
        }

        return $arrData;
    }
}
?>
